//玩家背包
let mongoose = require('mongoose');

let schema = new mongoose.Schema({
    items:[require('./gameUser.bag.item')]
});

schema.methods={
    getDescription(){
        return {
            101:'飞镖',
            201:'喇叭',
            202:'爱心'
        }
    },
    getMessage(options){
        let skins=[];
        for(let i=0;i<options.self.ppt.skins.length;i++){  //加入角色皮肤
            skins.push({type:options.self.ppt.skins[i],count:1});
        }
        for(let i=0;i<options.self.ppt.bombColors.length;i++){ //加入泡泡皮肤
            skins.push({type:options.self.ppt.bombColors[i]+500,count:1});
        }
        return {items:[].concat(this.items,skins)};
    },
    videoGet101(options){
        this.addItem(101,1);
    },
    //看视频加5个复活针
    videoAdd101(options){
        this.addItem(101,3);
    },
    //新增道具
    addItem(type,count){
        let item=this.items.find(item=>{return item.type===type});
        if(!!item){
            item.count+=count;
        }else{
            this.items.push({type:type,count:count});
        }
    },
    //减少道具
    reduceItem(type,count=1){
        let item=this.items.find(item=>{return item.type===type});
        if(!item||item.count<count){
            throw new Error('道具数量不足');
        }
        item.count-=count;
    },

    //判断是否有足够的道具
    hasItem(type,count=1){
        let item=this.items.find(item=>{return item.type===type});
        if(!item){
            return false;
        }
        return item.count>=count;
    },
    getCount(type){
        let item = this.items.find(item=>{return item.type===type});
        if(!item){
            this.items.push({type:type,count:0})
        }
        return item ? item.count : 0;
    }
};

module.exports=schema;